﻿using System;
using System.Data.Entity;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using Unity.Mvc3;
using WebProject.Core;

namespace WebProject
{
    public static class Bootstrapper
    {

        //显式获取UnityContainer 
        public static IUnityContainer GetContainerDirectly()
        {
            IUnityContainer container = new UnityContainer();
            //如果注册多个类型需要指定名字（名字是任意的）
            //否则当调用ResolveAll<>()时，那些没有指定名字的将会获取不到
            //但是，Resolve<>() 会默认取那些空名字的注入对象。
            //container.RegisterType<IProductionTypeService, ProductionTypeService>("productionTypeService");
            return container;
        }

        public static void Initialize()
        {
            try
            {
                var container = new UnityContainer().LoadConfiguration();
                DependencyResolver.SetResolver(new UnityDependencyResolver(container));
                // Entity Framework的默认初始化策略
                Database.SetInitializer(new CreateDatabaseIfNotExists<EfDbContext>());
            }
            catch (Exception ex)
            {
                string exMsg = ex.Message;
            }
        }

        //从配置文件获取UnityContainer 
        private static IUnityContainer GetContainerConfig()
        {
            IUnityContainer container = new UnityContainer();
            var section = (UnityConfigurationSection)System.Configuration.ConfigurationManager.GetSection("unity");
            section.Containers[0].Configure(container);
            return container;
        }

    }
}